package com.itheima.controller;

import com.itheima.domain.Result;
import com.itheima.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Controller
public class RestfulController {

    /**
     * post 请求：保存
     */
    @RequestMapping(value="/testRestful",method= RequestMethod.POST)
    @ResponseBody
    public Result testRestfulURLPOST(User user){
        System.out.println("rest post"+user);
        return new Result(true, "执行了保存数据操作");
    }
    /**
     * put 请求：更新
     * @return
     */
    @RequestMapping(value="/testRestful",method=RequestMethod.PUT)
    @ResponseBody
    public Result testRestfulURLPUT(User user){
        System.out.println("rest put  user--->"+user);
        return new Result(true, "执行了更新数据操作");
    }
    /**
     * DELETE 请求：删除
     * @return
     */
    @RequestMapping(value="/testRestful/{id}",method=RequestMethod.DELETE)
    @ResponseBody
    public Result testRestfulURLDELETE(@PathVariable("id")Integer id){
        System.out.println("rest delete "+id);
        return new Result(true, "执行了删除数据操作");
    }
    /**
     * GET 请求：查询
     * @return
     */
    @RequestMapping(value="/testRestful/{id}",method=RequestMethod.GET)
    @ResponseBody
    public Result testRestfulURLGET(@PathVariable("id")Integer id){
        System.out.println("rest get "+id);
        return new Result(true, "执行了查询数据操作");
    }



    @RequestMapping(value="/restful/{id}",method=RequestMethod.GET)
    public @ResponseBody User testGET(@PathVariable("id")Integer id, String username){
        System.out.println("rest get "+id);
        System.out.println("username "+username);

        User user = new User();
        user.setUname("黑马");
        user.setDate(new Date());
        return user;
    }
}
